﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    public class FileLoaderDAC
    {
        // TODO: вынести общий функционал для подобных классов доступа к БД в общий класс BaseDAC

        // Сама строка хранится в app.config (web.config) файле исполняемого файла, который дёрнет метод этой библиотеки классов.
        // В нашем случае это DigDes.DSchool.SUPS.Test.App файл App.Config.

        private static readonly string ConnectionStringKey = "SUPSDB";

        public static void DoSomething(MyLoader1DTO row)
        {
            
            
            if (row != null)
            {
                var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;
                using (var sqlConn = new SqlConnection(connectionString))
                {
                    sqlConn.Open();
                    using (var sqlCmd = sqlConn.CreateCommand())
                    {
                        sqlCmd.CommandText = "insert into tbDislocation " +
                       "([InventoryCarriageNumber],  [WeightTons],[DestinationStation_Code],[CargoCodeTariff_ShortCode],[ConsigneeCode], [ParkType],  [StartStation_Code],[OperationCode_Code],[OperationDate], [OperationYear], [OperationTime], [StationOfOperation_Code], [DeliveryRoad_Code],[ReceiptRoad_Code], [TrainIndex],[TrainNumber],[OperationType_Code] ) " +
                        "values (@train_carriage ,@weight ,@destination ,@сargo_code ,@consignee_code ,@park_type ,@start_station ,@opcode ,@opdate ,@opyear ,@optime ,@opstation ,@pass_road ,@receive_road ,@train_index,@train_numb,@optypecode )";
                        sqlCmd.Parameters.AddWithValue("@train_carriage", row.InventoryCarriageNumber);
                        sqlCmd.Parameters.AddWithValue("@weight", row.WeightTons);
                        sqlCmd.Parameters.AddWithValue("@destination", row.DestinationStation);
                        sqlCmd.Parameters.AddWithValue("@сargo_code",row.CargoCodeTariff);
                        sqlCmd.Parameters.AddWithValue("@consignee_code", row.ConsigneeCode);
                        sqlCmd.Parameters.AddWithValue("@park_type", row.ParkType);
                        sqlCmd.Parameters.AddWithValue("@start_station", row.StartStation);
                        sqlCmd.Parameters.AddWithValue("@opcode", row.OperationCode);
                        sqlCmd.Parameters.AddWithValue("@opdate", row.OperationDate);
                        sqlCmd.Parameters.AddWithValue("@opyear", row.OperationYear);
                        sqlCmd.Parameters.AddWithValue("@optime", row.OperationTime);
                        sqlCmd.Parameters.AddWithValue("@opstation", row.StationOfOperation);
                        sqlCmd.Parameters.AddWithValue("@pass_road", row.DeliveryRoad);
                        sqlCmd.Parameters.AddWithValue("@receive_road", row.ReceiptRoad);
                        sqlCmd.Parameters.AddWithValue("@train_index", row.TrainIndex);
                        sqlCmd.Parameters.AddWithValue("@train_numb", row.TrainNumber);
                        sqlCmd.Parameters.AddWithValue("@optypecode", 0); //не знаю по какому полю,пусть будет 0 ))
                        try
                        {
                            sqlCmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                        
                        
                    }
                    sqlConn.Close();
                }
            }
                        
        }

        public static void DoSomething2(IEnumerable<MyLoader1DTO> rows)
        {
            // Делается по аналогии с предыдущим методом
            throw new NotImplementedException();
        }
    }
}
